#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fileencoding=utf-8
# vim:tabstop=4
#
# copyright (c) 2010 by The Codedrawer Team
#
# 成员
# xkhhdx(HD.Huo)	viking9345(C.Cai)	renxing225(X.Ren)
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# 代码来自：http://code.google.com/p/codedrawer/
#
#--- 更新记录 ---
#成员		时间			简述
#xkhhdx		4:02 2010-8-30	添加对 PHP 的支持
#
#--- 计划列表 ---
#提出者		时间			是否解决	解决者	计划简述		解决简述
#

from core import *

class php_code_render(code_render):
    """ 对 php 代码进行标记 """
    def prepare(self):
        """ 准备正则式、正则库等信息 """
        
        self.regex_dict["comment"] = (	r'''(/\*[\s\S]*?\*/)''',						# C 风格的块注释
                                      	r'''(//.*(?=\n))|(//.*$)''',					# C++ 风格的行注释
                                      	#r'''(#.*(?=\n))|(#.*$)''',						# Perl 风格的行注释
                                      	#r"""((?:\'\')'(?:\.|(\\\')|[^\''\n]|)+''')""",	# python 三个单引号
                                      	r"""('(?:\.|(\\\')|[^\''\n]|)+')""")			# 单引号引起来的
        
        # 字符串
        self.regex_dict["quoted_string"] = (	#r'''((?:\"\")"(?:\.|(\\\")|[^\""\n]|)+""")''',	# python 三个双引号
                                            	r'''("(?:\.|(\\\")|[^\""\n]|)+")''',			# 双引号引起来的
                                            	r'''(&quot;(?:\.|(\\&quot;)|[^\n]|)+&quot;)''')	# 双引号引起来的

        # 关键字
        keyword_list = ( "and", "or", "xor", "__FILE__", "__LINE__", "array", "as", "break", "case", "cfunction", "class", "const", "continue", "declare", "default", "die", "do", "else", "elseif", "empty", "enddeclare", "endfor", "endforeach", "endif", "endswitch", "endwhile", "extends", "for", "foreach", "function", "include", "include_once", "global", "if", "new", "old_function", "return", "static", "switch", "use", "require", "require_once", "var", "while", "__FUNCTION__", "__CLASS__", "__METHOD__", "abstract", "interface", "public", "implements", "extends", "private", "protected", "throw" )

        # 预定义的函数
        func_type = ( "abs", "acos", "acosh", "addcslashes", "addslashes", "array_change_key_case", "array_chunk", "array_combine", "array_count_values", "array_diff", "array_diff_assoc", "array_diff_key", "array_diff_uassoc", "array_diff_ukey", "array_fill", "array_filter", "array_flip", "array_intersect", "array_intersect_assoc", "array_intersect_key", "array_intersect_uassoc", "array_intersect_ukey", "array_key_exists", "array_keys", "array_map", "array_merge", "array_merge_recursive", "array_multisort", "array_pad", "array_pop", "array_product", "array_push", "array_rand", "array_reduce", "array_reverse", "array_search", "array_shift", "array_slice", "array_splice", "array_sum", "array_udiff", "array_udiff_assoc", "array_udiff_uassoc", "array_uintersect", "array_uintersect_assoc", "array_uintersect_uassoc", "array_unique", "array_unshift", "array_values", "array_walk", "array_walk_recursive", "atan", "atan2", "atanh", "base64_decode", "base64_encode", "base_convert", "basename", "bcadd", "bccomp", "bcdiv", "bcmod", "bcmul", "bindec", "bindtextdomain", "bzclose", "bzcompress", "bzdecompress", "bzerrno", "bzerror", "bzerrstr", "bzflush", "bzopen", "bzread", "bzwrite", "ceil", "chdir", "checkdate", "checkdnsrr", "chgrp", "chmod", "chop", "chown", "chr", "chroot", "chunk_split", "class_exists", "closedir", "closelog", "copy", "cos", "cosh", "count", "count_chars", "date", "decbin", "dechex", "decoct", "deg2rad", "delete", "ebcdic2ascii", "echo", "empty", "end", "ereg", "ereg_replace", "eregi", "eregi_replace", "error_log", "error_reporting", "escapeshellarg", "escapeshellcmd", "eval", "exec", "exit", "exp", "explode", "extension_loaded", "feof", "fflush", "fgetc", "fgetcsv", "fgets", "fgetss", "file_exists", "file_get_contents", "file_put_contents", "fileatime", "filectime", "filegroup", "fileinode", "filemtime", "fileowner", "fileperms", "filesize", "filetype", "floatval", "flock", "floor", "flush", "fmod", "fnmatch", "fopen", "fpassthru", "fprintf", "fputcsv", "fputs", "fread", "fscanf", "fseek", "fsockopen", "fstat", "ftell", "ftok", "getallheaders", "getcwd", "getdate", "getenv", "gethostbyaddr", "gethostbyname", "gethostbynamel", "getimagesize", "getlastmod", "getmxrr", "getmygid", "getmyinode", "getmypid", "getmyuid", "getopt", "getprotobyname", "getprotobynumber", "getrandmax", "getrusage", "getservbyname", "getservbyport", "gettext", "gettimeofday", "gettype", "glob", "gmdate", "gmmktime", "ini_alter", "ini_get", "ini_get_all", "ini_restore", "ini_set", "interface_exists", "intval", "ip2long", "is_a", "is_array", "is_bool", "is_callable", "is_dir", "is_double", "is_executable", "is_file", "is_finite", "is_float", "is_infinite", "is_int", "is_integer", "is_link", "is_long", "is_nan", "is_null", "is_numeric", "is_object", "is_readable", "is_real", "is_resource", "is_scalar", "is_soap_fault", "is_string", "is_subclass_of", "is_uploaded_file", "is_writable", "is_writeable", "mkdir", "mktime", "nl2br", "parse_ini_file", "parse_str", "parse_url", "passthru", "pathinfo", "readlink", "realpath", "rewind","rewinddir", "rmdir", "round", "str_ireplace", "str_pad", "str_repeat", "str_replace", "str_rot13", "str_shuffle", "str_split", "str_word_count", "strcasecmp","strchr", "strcmp", "strcoll", "strcspn", "strftime", "strip_tags", "stripcslashes", "stripos", "stripslashes", "stristr", "strlen", "strnatcasecmp", "strnatcmp", "strncasecmp", "strncmp", "strpbrk", "strpos", "strptime", "strrchr", "strrev", "strripos", "strrpos", "strspn", "strstr", "strtok", "strtolower", "strtotime", "strtoupper", "strtr", "strval", "substr", "substr_compare" )

        # 第一个是变量……
        self.regex_dict["keyword"] = r"(\$\w+|\b" + r"\b|\b".join(keyword_list) + r"\b|\b".join(func_type) + r"\b)"

        self.verifier_dict["spliter"] = re.compile( "|".join(self.regex_dict["comment"]) + r"|" + "|".join(self.regex_dict["quoted_string"]))
        self.verifier_dict["comment"] =  re.compile( "|".join(self.regex_dict["comment"]) )
        self.verifier_dict["quoted_string"] =  re.compile( "|".join(self.regex_dict["quoted_string"]) )
        self.verifier_dict["keyword"] = re.compile(self.regex_dict["keyword"])

